﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.Timers;
using library;
using System.IO;

namespace CommCenter
{
    public partial class Form1 : Form
    {
        public static string pbImg;
        public String[] errorLog = new String[100];
        public static int errorCheck = 0;
        static string cwdd = System.IO.Directory.GetCurrentDirectory();
        string cwd = System.IO.Directory.GetDirectoryRoot(cwdd);
        static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cwd += "C#\\MD Final\\Files\\";

            boxNukeC.Text = library.Missilecount.Nuke.ToString();
            boxJavC.Text = library.Missilecount.Javelin.ToString();
            boxTomaC.Text = library.Missilecount.Tomahawk.ToString();
            boxBunkC.Text = library.Missilecount.BunkerBuster.ToString();
            lblTarName.Visible = false;
            lblLevel.Visible = false;
            boxDist2.Visible = false;
            boxSpeed2.Visible = false;
            lblName.Text = "M-Com 4800";
            btnFire.Enabled = false;
            pnlYellow.Visible = false;
            pnlRed.Visible = false;
            picBox.Image = CommCenter.Properties.Resources.communications_center;
            
            myTimer.Tick += new EventHandler(timerRun);  
            myTimer.Interval = 1000;   
            myTimer.Start();    
        }

        private void timerRun(object source, EventArgs myEventArgs)
         {
            myTimer.Stop(); 
            LON RAD_Event = new LON();
            Status RAD_Status = new Status();
            Status MIS_Status = new Status();
            try
            {
                XmlReader reader = new XmlTextReader(cwd + "RAD_Event.xml");  
                XmlSerializer serializer = new XmlSerializer(typeof(LON));  
                RAD_Event = (LON)serializer.Deserialize(reader);  
                reader.Close();  
                boxTarg.Text = RAD_Event.Targ.ToString();
                boxSpeed.Text = RAD_Event.Speed;
                boxSpeed2.Text = RAD_Event.Speed;
                boxDist.Text = RAD_Event.Distance.ToString();
                boxDirect.Text = RAD_Event.Direction.ToString();
                boxDist2.Text = RAD_Event.Distance.ToString();
                boxThreat.Text = RAD_Event.ThreatLevel.ToString();
                txtMissile.Text = RAD_Event.Missile.ToString();
                lblRadarTask.Text = RAD_Event.Status;

                changePicture(RAD_Event.Targ.ToString());
                findSuccess(RAD_Event.Distance.ToString(), RAD_Event.Speed);

                lblTarName.Visible = true;
                lblLevel.Visible = true;
                boxDist2.Visible = true;
                boxSpeed2.Visible = true;
                lblNone.Visible = false;
                pnlGreen.Visible = false;
                pnlYellow.Visible = true;
                btnFire.Enabled = true;
            }

            catch (Exception ex)
            {
                //errorLog[errorCheck].
            }

            try
            {
                XmlReader reader = new XmlTextReader(cwd + "RAD_Status.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(Status));
                RAD_Status = (Status)serializer.Deserialize(reader);
                reader.Close();

                lblOnOffR.Text = "Online";
                lblOnOffR.ForeColor = Color.Green;
            }

            catch (Exception off)
            {
                lblOnOffR.Text = "Offline";
                lblOnOffR.ForeColor = Color.Red;
            }

            try
            {
                XmlReader reader = new XmlTextReader(cwd + "MIS_Status.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(Status));
                MIS_Status = (Status)serializer.Deserialize(reader);
                reader.Close();

                lblOnOffL.Text = "Online";
                lblOnOffL.ForeColor = Color.Green;
            }

            catch (Exception off)
            {
                lblOnOffL.Text = "Offline";
                lblOnOffL.ForeColor = Color.Red;
            }
            
            myTimer.Start();
        }

        public void changePicture(string target)
        {
            switch (target)
            {
                case "NicksCar":
                    picBox.Image = CommCenter.Properties.Resources.nickCar2_copy;
                    break;
                case "HuisCar":
                    picBox.Image = CommCenter.Properties.Resources.carHui2_copy;
                    break;
                case "SpencersTruck":
                    picBox.Image = CommCenter.Properties.Resources.truckTarget;
                    break;
                case "CeilingComputer":
                    picBox.Image = CommCenter.Properties.Resources.ceilComp_copy;
                    break;
                case "NicksCarHit":
                    picBox.Image = CommCenter.Properties.Resources.nickCarBlow_copy;
                    break;
                case "HuisCarHit":
                    picBox.Image = CommCenter.Properties.Resources.carHuiBlow_copy;
                    break;
                case "SpencersTruckHit":
                    picBox.Image = CommCenter.Properties.Resources.truckBlow_copy;
                    break;
                case "ComputerHit":
                    picBox.Image = CommCenter.Properties.Resources.ceilCompBlow_copy;
                    break;
                case "NicksCarMiss:":
                    picBox.Image = CommCenter.Properties.Resources.nickCarMiss_copy;
                    break;
                case "HuisCarMiss:":
                    picBox.Image = CommCenter.Properties.Resources.carHuiMiss_copy;
                    break;
                case "SpencersTruckMiss":
                    picBox.Image = CommCenter.Properties.Resources.truckMiss;
                    break;
                case "ComputerMiss":
                    picBox.Image = CommCenter.Properties.Resources.ceilCompMiss_copy;
                    break;
                default:
                    break;
            }
            
        }

        //public void Timer2(int TimeToWait)
        //{
        //    //5000 would be 5 seconds
        //    Timer myTimer = new Timer(TimeToWait);
        //    myTimer.Elapsed += new ElapsedEventHandler(ElapsedEvent);
        //    myTimer.Enabled = true;
        //}

        //public void ElapsedEvent(object source, ElapsedEventArgs e)
        //{
        //    picBox.Image = CommCenter.Properties.Resources.nickCar1_copy;
        //}

        private void findSuccess(string xS, string yS)
        {
            double rate = 0;
            double x = Double.Parse(xS);
            double y = Double.Parse(yS);

            //Nuke
            boxNuke.Text = "100";

            //Bunker Buster
            rate = (100*x) / (x + ((.0025*x) * y));
            boxBunk.Text = rate.ToString();

            //Javelin
            rate = (100 * y) / (y + ((.009 * y) * x));
            boxJav.Text = rate.ToString();

            //Tomahawk
            rate = (100 * y) / (y + ((.0015 * y) * x));
            boxToma.Text = rate.ToString();
        }

        private void btnFire_Click_1(object sender, EventArgs e)
        {
            myTimer.Stop();
            pnlYellow.Visible = false;
            pnlRed.Visible = true;
            lblLaunchStat.Text = "Launching";
 
            MissileLaunch COMM_Event = new MissileLaunch();

            if (txtMissile.Text == "Nuke")
            {   
                COMM_Event.Percentage = "100";
                Missilecount.Nuke--;
                COMM_Event.Missile = MissileLaunch.MissileList.Nuke;
            }
            else if (txtMissile.Text == "BunkerBuster")
            {   
                COMM_Event.Percentage = boxBunk.Text;
                Missilecount.BunkerBuster--;
                COMM_Event.Missile = MissileLaunch.MissileList.BunkerBuster;
            }
            else if (txtMissile.Text == "Javelin")
            {   
                COMM_Event.Percentage = boxJav.Text;
                Missilecount.Javelin--;
                COMM_Event.Missile = MissileLaunch.MissileList.Javelin;
            }
            else if (txtMissile.Text == "Tomahawk")
            {   
                COMM_Event.Percentage = boxToma.Text;
                Missilecount.Tomahawk--;
                COMM_Event.Missile = MissileLaunch.MissileList.Tomahawk; 
            }

            COMM_Event.Targ = boxTarg.Text;
            COMM_Event.Speed = Convert.ToInt32(boxSpeed.Text);

            boxNukeC.Text = library.Missilecount.Nuke.ToString();
            boxJavC.Text = library.Missilecount.Javelin.ToString();
            boxTomaC.Text = library.Missilecount.Tomahawk.ToString();
            boxBunkC.Text = library.Missilecount.BunkerBuster.ToString();

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(MissileLaunch));
                TextWriter textWriter = new StreamWriter(cwd + "COMM_Event.xml");
                serializer.Serialize(textWriter, COMM_Event);
                textWriter.Close();
            }
            catch (Exception ex)
            {
                //errorLog[errorCheck].Equals(ex);
                //errorCheck++;
                MessageBox.Show("Problem");
            }
            myTimer.Start();
        }

        private void shutDownToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
